//
// Writer: Zooper
//
// Date  : 01.05.2006
//

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>

/******************************************************************/
             //1 2 3 4 5
int row1[5] = {0,1,1,0,0};
int row2[5] = {0,0,0,1,1};
int row3[5] = {0,0,0,0,0};
int row4[5] = {1,0,0,0,0};
int row5[5] = {0,0,1,1,0};

int* map[5] = {row1, row2, row3, row4, row5};

struct mrec
{
  int us;
  mrec *next;
};

mrec *mmat[5];

/******************************************************************/
typedef struct st
{
  int dann;
  struct st *prev;
}STACK;

STACK *top = NULL;

/******************************************************************/
void push(int a)
{
  STACK *q;
  q = new STACK;
  q->dann = a;
  q->prev = top;
  top = q;
}

int pop(void)
{
  STACK *q;
  int temp;
  if (top == NULL)
  {
    cout<< "\nSTACK empty.";
  }
  else
  {
    temp = top->dann;
    q = top->prev;
    delete top;
    top = q;
  }
  return temp;
}

void clear(void)
{
  STACK *q;
  while (top != NULL)
  {
    q = top->prev;
    delete top;
    top = q;
  }
}

/******************************************************************/
bool first = true;

void make_mrec(int** map)
{
  mrec *ptr;
  for (int i=0; i<5; i++)
  {
    first = true;
    for (int j=0; j<5; j++)
    {
      if (map[i][j] != 0)
      {
        if (first == true)
        {
          first = false;
          ptr = new mrec;
          mmat[i] = ptr;
          ptr->next = NULL;
          ptr->us = j;
        }
        else
        {
          ptr->next = new mrec;
          ptr = ptr->next;
          ptr->us = j;
          ptr->next = NULL;
        }
      }
    }
  }
}

int *visited = new int[5];
void depth_first_search(void)
{
  while (top != NULL)
  {
    int w = pop();
    if (visited[w] == 0)
    {
      visited[w] = 1;
      cout << w << " ";
      while (w)
      {
        push(w);
      }
    }
  }
}

/******************************************************************/
void main(void)
{
  clrscr();
  for (int j=0; j<5; j++)
  (
    mmat
  )
  for (int k=0; k<5; k++)
  {
    visited[k] = 0;
  }
  push(map[0][0]);
  depth_first_search();
  getch();
}

